package com.jz.digital.generate_get

import com.jz.digital.MyAppLocation
import com.jz.digital.base.BasePresenter
import com.jz.digital.bean.BaseEntity
import com.jz.digital.bean.ServerBean
import com.jz.digital.listener.ApiCallback
import com.jz.digital.net.DataApi
import com.jz.digital.utils.AccountManagerHelper
import com.jz.digital.utils.AppUtil
import com.jz.digital.utils.ErrorCode
import com.jz.digital.utils.GreenDaoUtils
import com.jz.digital.utils.GsonUtil
import com.jz.digital.utils.LogUtils

/**
 * @author jiangdz
 * @date 2024/11/19
 * @time 19:26
 * 用途:生产领料
 */
class MaterialRequisitionCommitPresenter : BasePresenter<IMaterialRequisitionCommitView>() {
    private val tag = "MaterialRequisitionCommitPresenter"

    fun initData() {
    }

    fun commit(
        barcode: String?, no1: String?, no2: String?, no3: String?,
        no4: String, no5: String, no6: String?, no7: String,
        no8: String, no9: String, qty1: Int, qty2: Int, qty3: Int,
        qty4: Int, exit: Boolean
    ) {
        LogUtils.d(tag, "开始提交")
        view?.showLoading()
        val userInfo = GreenDaoUtils.getUserInfo(AccountManagerHelper.getInstance().userId)
        DataApi.api.shengChangLinLiao(
            ServerBean.ShengChangLinLiaoRequest(
                mode = 2,
                barcode = barcode,
                wk_id = userInfo?.userId,
                wk_name = userInfo?.userName,
                pda = AppUtil.getIMEI(MyAppLocation.mContext),
                no1 = no1,
                no2 = no2,
                no3 = no3,
                no4 = no4,
                no5 = no5,
                no6 = no6,
                no7,
                no8,
                no9,
                qty1,
                qty2,
                qty3,
                qty4
            )
        ).enqueue(object : ApiCallback<BaseEntity<ServerBean.ShengChangLinLiaoResponse?>>() {
            override fun onSuccess(response: BaseEntity<ServerBean.ShengChangLinLiaoResponse?>?) {
                view?.dismissLoading()
                LogUtils.d(tag, "提交成功 ${GsonUtil.toJson(response)}")
                if (response != null) {
                    if (response.code == ErrorCode.CODE_NOT_ALLOW_SIMULTANEOUS_RECEIPT) {
                        view?.commitFailed(response.message ?: "", exit)
                    }
                    if (response.data != null) {
                        view?.commitSuccess(response.data!!, exit)
                    }
                }

            }

            override fun onError(t: Throwable?, code: Int, message: String?) {
                view?.dismissLoading()
                LogUtils.d(tag, "提交失败 t=${t?.message},code=$code,message=$message")
                view?.commitFailed(message ?: "", exit)
            }

        })
    }

}